#include "defs.h"
#include "filter.h"

Config_T * g_configPtr = 0;
char g_nativeFilename[0x100] = { 0 };

BOOL __stdcall DllMain
(
	HINSTANCE hInstance, 
	DWORD dwReason, 
	void *
)
{
	if( dwReason == DLL_PROCESS_ATTACH )
	{
		DisableThreadLibraryCalls(hInstance);
		FillMemory(g_nativeFilename, sizeof(g_nativeFilename), 0);
		GetModuleFileNameA(hInstance, g_nativeFilename, sizeof(g_nativeFilename) - 1);
	}
	return TRUE;
}

BOOL __stdcall GetFilterVersion
(
	HTTP_FILTER_VERSION * pVer
)
{
	return OnGetFilterVersion(g_nativeFilename, &g_configPtr, pVer);
}

DWORD __stdcall HttpFilterProc
(
	PHTTP_FILTER_CONTEXT pFC, 
	DWORD dwNotifyType, 
	void * pvNotify
)
{
	DWORD ret = SF_STATUS_REQ_NEXT_NOTIFICATION;

	switch( dwNotifyType )
	{
		case SF_NOTIFY_READ_RAW_DATA:
			ret = OnReadRawData(g_configPtr, pFC, (PHTTP_FILTER_RAW_DATA)pvNotify, dwNotifyType);
			break;

		case SF_NOTIFY_PREPROC_HEADERS:
			ret = OnPreProcHeaders(g_configPtr, pFC, (PHTTP_FILTER_PREPROC_HEADERS)pvNotify, dwNotifyType);
			break;

		case SF_NOTIFY_URL_MAP:
			ret = OnUrlMap(g_configPtr, pFC, (PHTTP_FILTER_URL_MAP)pvNotify, dwNotifyType);
			break;

		case SF_NOTIFY_AUTHENTICATION:
			ret = OnAuthentication(g_configPtr, pFC, (PHTTP_FILTER_AUTHENT)pvNotify, dwNotifyType);
			break;

		case SF_NOTIFY_ACCESS_DENIED:
			ret = OnAccessDenied(g_configPtr, pFC, (PHTTP_FILTER_ACCESS_DENIED)pvNotify, dwNotifyType);
			break;

		case SF_NOTIFY_AUTH_COMPLETE:
			ret = OnAuthComplete(g_configPtr, pFC, (PHTTP_FILTER_AUTH_COMPLETE_INFO)pvNotify, dwNotifyType);
			break;

		case SF_NOTIFY_SEND_RESPONSE:
			ret = OnSendResponse(g_configPtr, pFC, (PHTTP_FILTER_SEND_RESPONSE)pvNotify, dwNotifyType);
			break;

		case SF_NOTIFY_SEND_RAW_DATA:
			ret = OnSendRawData(g_configPtr, pFC, (PHTTP_FILTER_RAW_DATA)pvNotify, dwNotifyType);
			break;

		case SF_NOTIFY_END_OF_REQUEST:
			ret = OnEndOfRequest(g_configPtr, pFC, dwNotifyType);
			break;

		case SF_NOTIFY_LOG:
			ret = OnLog(g_configPtr, pFC, (PHTTP_FILTER_LOG)pvNotify, dwNotifyType);
			break;

		case SF_NOTIFY_END_OF_NET_SESSION:
			ret = OnEndOfNetSession(g_configPtr, pFC, dwNotifyType);
			break;
	}

	if( SF_STATUS_REQ_ERROR == ret )
	{
		SetLastError(GetLastError());
	}

	return ret;
}

BOOL __stdcall TerminateFilter
(
	DWORD
)
{
	return OnTerminateFilter(g_configPtr);
}
